package com.example.pm.ending.server.service.ec.ownermodule;

import com.example.pm.ending.server.ex.ServiceException;
import com.example.pm.ending.server.mapper.*;
import com.example.pm.ending.server.pojo.vo.HouseStandardVO;
import com.example.pm.ending.server.pojo.vo.OwnerStandardVO;
import com.example.pm.ending.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.example.pm.ending.server.service.IOwnerModuleService.HOUSE_STATE_IDLE;

@Component
@Slf4j
public class DeleteOwnerES {

    @Autowired
    public OwnerMapper ownerMapper;
    @Autowired
    public HouseMapper houseMapper;
    @Autowired
    public CarportMapper carportMapper;
    @Autowired
    public PayMapper payMapper;
    @Autowired
    public PublishOwnerMapper publishOwnerMapper;



    public void deleteOwner(Long ownerId){
        log.debug("开始处理[删除业主]业务");
        log.debug("删除的业主id为：{}",ownerId);
        ownerService(ownerId);

        houseService(ownerId);

        carportService(ownerId);

        payService(ownerId);

        pushOwnerService(ownerId);

    }

    /**
     * 处理owner表
     * @param ownerId 业主id
     */
    public void ownerService(Long ownerId){
        log.debug("开始处理owner表");
        log.debug("传递过来的参数:{}",ownerId);

        OwnerStandardVO ownerVO = ownerMapper.getOwnerById(ownerId);
        log.debug("ownerVO:{}",ownerVO);
        //判断删除的业主是否存在 若不存在抛出异常
        if(ownerVO==null){
            String message = "删除的业主不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        ownerMapper.deleteById(ownerId);
    }

    /**
     * 处理house表
     * @param ownerId 业主id
     */
    public void houseService(Long ownerId){
        log.debug("处理house表");
        log.debug("传递过来的参数:{}",ownerId);
        //业主删除，应将房产的状态变为`空闲` 以及 所属业主为`null`
        HouseStandardVO houseVO = houseMapper.getHouseByOwnerId(ownerId);
        //判断修改的房产信息是否存在
        //由于业务要求业主是必须有房产的  所以业主没有房产则会抛出异常
        if(houseVO==null){
            String message = "数据异常,请刷新试试";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        houseMapper.updateOwnerIdAndStateIdByOwnerId(null,HOUSE_STATE_IDLE,ownerId);
    }

    /**
     * 处理carport表
     * @param ownerId 业主id
     */
    public void carportService(Long ownerId){
        log.debug("处理carport表");
        log.debug("传递过来的参数:{}",ownerId);
        //业主删除,应将业主的车库所属业主改为`null`,若业主没有车库,以下不执行修改语句
        int countCarport = carportMapper.countByOwnerId(ownerId);
        if(countCarport==0){
            return;
        }
        carportMapper.updateOwnerIdByOwnerId(null, ownerId);
    }

    /**
     * 处理pay表
     * @param ownerId 业主id
     */
    public void payService(Long ownerId){
        log.debug("处理pay表");
        log.debug("传递过来的参数:{}",ownerId);
        //业主删除,应将业主的缴费信息删除,若业主没有订单信息,以下不执行删除语句
        int countPay = payMapper.countByOwnerId(ownerId);
        if(countPay==0){
            return;
        }
        payMapper.deleteByOwnerId(ownerId);
    }

    /**
     * 处理push_owner表
     * @param ownerId 业主id
     */
    public void pushOwnerService(Long ownerId){
        log.debug("处理push_owner表");
        log.debug("传递过来的参数:{}",ownerId);
        //业主删除,应将业主的公布信息删除,若业主没有公布信息,以下不执行删除语句
        int countPublishOwner = publishOwnerMapper.countByOwnerId(ownerId);
        if(countPublishOwner==0){
            return;
        }
        publishOwnerMapper.deleteByOwnerId(ownerId);
    }

}
